<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
<style>
      body {
      height:2000px;
    }
</style>
</head>

<body>
  <input type="text">

  <script>
    //防抖：用户触发时间过于频繁，只要最后一次事件的操作
    let inp = document.querySelector("input");
    let t = null;//计时器的t控制是否触发

    inp.oninput = function(){
      if(t !== null){
        clearTimeout(t);//把之前事件清除
      }
      t = setTimeout(() => { //每次输入 触发计时器 
        console.log(this.value)  //如果是最后一次就会触发
      },500)
    }

    //封装 call 闭包

    let inp = document.querySelector("input");

    inp.oninput = debounce(function () {
      console.log(this.value)
    }, 500)

    function debounce(fn, delay) {
      let t = null; 
      return function () {
        if (t !== null) {
          clearTimeout(t); 
        }
        t = setTimeout(() => { 
          fn().call(this); 
        }, delay)
      }
    }

  ///节流
  let flag = true;
    window.onscroll = function(){
      if(flag){
        setTimeout(() => {
          console.log("123")
          flag = true;
        },500)
      }
      flag = false;
    }

  </script>
</body>

</html>